package cn.edu.neusoft.yuejian13.yuejian_13.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.yuejian13.yuejian_13.R;
import cn.edu.neusoft.yuejian13.yuejian_13.javaBean.Success;
import cn.edu.neusoft.yuejian13.yuejian_13.listener.TListener;
import cn.edu.neusoft.yuejian13.yuejian_13.model.LoginModel;


/**
 * Created by Lix on 2018/9/27.
 */

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_reg, btn_back;
    private TextView reg_name, reg_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
        initEvent();

    }
    TListener<Success> listener =new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            if (success.getError().equals("1")){
                Toast.makeText(RegisterActivity.this,"此用户已存在!!", Toast.LENGTH_SHORT).show();
            }else if(success.getError().equals("0")){
                Toast.makeText(RegisterActivity.this,"注册成功!!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }else  Toast.makeText(RegisterActivity.this,"注册失败，请稍后再试!!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initViews() {
        btn_reg = (Button) findViewById(R.id.reg_btn);
        btn_back = (Button) findViewById(R.id.regback_btn_);
        reg_name = (TextView) findViewById(R.id.reg_name);
        reg_pwd = (TextView) findViewById(R.id.reg_pwd);
    }

    private void initEvent() {
        btn_reg.setOnClickListener(this);
        btn_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.regback_btn_:
                finish();
                break;
            case R.id.reg_btn:
                String name = reg_name.getText().toString();
                String pwd = reg_pwd.getText().toString();
                LoginModel loginModel = new LoginModel();
                loginModel.register(name,pwd,listener);
                break;

        }
    }
}

